package com.china08.yxyapi.api.manage.tec.office.innernotice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;

/**
 * 老师发送内部通知 已读状态修改
 * 
 */
@CrossOrigin
@RestController("TeaInnerNoticeUpdIsReadController")
public class TeaInnerNoticeUpdIsReadController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @RequestMapping(value = "/api_yw/manage/tea/office/innerNotice/receive/updRead", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        String currentUserId = getProfile(request, response).getId();
        int isRead = 0;
        List<YxyNoticeInner> yxyNoticeInnerLst = yxyNoticeInnerRepository
                .findByIdAndReceiverUserIdAndReceiverIsReadAndSchoolId(reqModel.id, currentUserId, isRead, schoolId);
        for (YxyNoticeInner yxyNoticeInner : yxyNoticeInnerLst) {
            List<YxyNotifictionTea> YxyNotifictionTealist = yxyNoticeInner.getReceiver();
            for (YxyNotifictionTea yxyNotifictionTea : YxyNotifictionTealist) {
                if (yxyNotifictionTea.getIsRead() == 0 && currentUserId.equals(yxyNotifictionTea.getUserId())) {
                    yxyNotifictionTea.setIsRead(1);
                }
            }
        }
        yxyNoticeInnerRepository.save(yxyNoticeInnerLst);
        RespModel respModel = new RespModel();
        respModel.setMessage("已读");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 消息id
        public String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}